package com.example.springandtiles.controller;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.springandtiles.model.Player;
import com.example.springandtiles.service.PlayersService;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/dashboard")
public class DashboardController {

    private static final Logger logger = LoggerFactory.getLogger(DashboardController.class);

    @Autowired
    PlayersService playersService;

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Seeing dashboard! The client locale is {}.", locale);

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate);

        return "dashboard";
    }

    @RequestMapping(value = "/ranking", method = RequestMethod.GET)
    @ResponseBody
    public List<Player> retrieveRanking(Locale locale) {
        logger.info("Retrieving ranking! The client locale is {}.", locale);

        playersService.retrieveRankedPlayers();

        List<Player> rankingList = new ArrayList();
        Player player = new Player(1, "Player 1", 100, 0, 5);
        rankingList.add(player);
        player = new Player(2, "Player 2", 90, 0, 5);
        rankingList.add(player);

        return rankingList;
    }

}
